<?php

namespace App\Http\Controllers;

use App\Extend\Regex;
use App\Extend\StringEx;
use App\Extend\UserIDInfo;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests,UserIDInfo;



    /**
     * 检测字段规则
     * 1.不能为空
     * 2.只能为数字，字母和特殊字符（[^\x21-\x7e]）
     * @param $filed
     * @return bool
     *
     */
    protected function CheckFileds(&$filed)
    {
        if($filed===null) $filed="";
        $filed=trim($filed);
        if($filed ==="" ) return false;
        if(Regex::IsMatch($filed,'[^\x21-\x7e]')) return false;
        $filed=trim($filed);
        return true;
    }


    protected function getValueJson(&$val,$obj)
    {
        try
        {
            $val=$obj->$val;
        }catch (\Exception $ex)
        {
            $val="";
        }

    }

    protected function getValueForm(&$val)
    {
        try
        {
            $val=$_POST[$val];
        }catch (\Exception $ex)
        {
            $val="";
        }

    }



    protected function getValuesFromPost(&...$value)
    {

        if($value==null||count($value)<=0) return;

        $type=request()->getContentType();

        try {

            switch ($type) {
                case "form":
                    foreach($value as &$var)
                    {
                        $this->getValueForm($var);
                    }
                    break;

                case "json":
                    $str = json_decode(\request()->getContent());
                    foreach($value as &$var)
                    {
                        $this->getValueJson($var,$str);
                    }
                    break;

                default:
                    break;

            }
        }catch (\Exception $ex)
        {
            $ex->getMessage();
        }

    }



    function getParameters()
    {
        $method=request()->getMethod();

        $strParameters=null;

        switch (strtoupper($method))
        {
            case "GET":
            case "DELETE":
                $strParameters=json_encode($_GET);
                break;

            case "POST":
            case "PUT":
                $type=request()->getContentType();
                switch (strtoupper($type)) {
                    case "FORM":
                        $strParameters=json_encode($_POST);
                        break;

                    case "JSON":
                        $strParameters=request()->getContent();
                        break;

                    default:
                        $strParameters= null;
                        break;
                }

                break;

            default:
                $strParameters= null;
                break;

        }

        if($strParameters)
        {
            //1.更改 "(%22)为 '(\")
            $strParameters1= Regex::Replace($strParameters,'~((?!\\\\).){1}(?=%22)~','$0\\');


            return json_decode(urldecode($strParameters1));
        }else
        {
            return null;
        }

    }







}
